# 1. 如何提取单个页面的数据
# 2. 上线程池，多个页面同时抓取

from concurrent.futures import ThreadPoolExecutor
import requests


def download_onePage(page, url):
    data = {
        'limit': 10,
        'current': int(page),
        'pubDateStartTime': '',
        'pubDateEndTime': '',
        'prodPcatid': '',
        'prodCatid': '',
        'prodName': ''
    }
    resp = requests.post(url, data=data)
    resp.close()
    print(resp.text)
    print(resp.json()['list'])

    dic = resp.json()['list']
    print(dic[0]['prodCat'])
    print(dic[0]['prodName'])
    print(dic[0]['lowPrice'])
    print(dic[0]['highPrice'])
    print(dic[0]['unitInfo'])
    print(dic[0]['pubDate'])
    print(dic[0]['place'])


if __name__ == '__main__':
    """
    # 效率很低
    for i in range(1, 10):
        download_onePage(i, 'http://www.xinfadi.com.cn/getPriceData.html')
    """
    # 创建线程池
    with ThreadPoolExecutor(50) as th:
        for i in range(1, 10):
            th.submit(download_onePage, i, 'http://www.xinfadi.com.cn/getPriceData.html')
    # 等待线程池中的任务全部执行完毕，才会继续执行守护线程
    print('over!')
